#!/bin/sh
# Copyright (c) The Exim Maintainers 1995 - 2018

set -e
LC_ALL=C
export LC_ALL

# Update Exim's version header file.

# Compatibility gross-ness for non-POSIX systems
if [ -z "$EXIM_REVERSION_ADJUSTED" ]
then
  SHELL=/bin/sh
  EXIM_REVERSION_ADJUSTED=yes
  export SHELL EXIM_REVERSION_ADJUSTED
  # Solaris:
  if [ -x /usr/xpg4/bin/sh ]
  then
    PATH="/usr/xpg4/bin:$PATH"
    SHELL=/usr/xpg4/bin/sh
    export PATH SHELL
  fi
  # Irix:
  _XPG=1 ; export _XPG
  #
  exec "$SHELL" "$0" "$@"
fi

# Read version information that was generated by a previous run of
# this script, or during the release process.

if   [ -f ./version.sh ]; then
    .    ./version.sh
elif [ -f ../src/version.sh ]; then
    .    ../src/version.sh
elif [ -d ../../.git ] || [ -f ../../.git ] || [ "$1" = release ]; then
    # Modify the output of git describe into separate parts for
    # the name "exim" and the release and variant versions.
    # Put a dot in the version number and remove a spurious g.
    if [ "$2" ]
    then
        description=$(git describe "$2")
    else
        description=$(git describe --dirty=-XX --match 'exim-4*')
    fi
    set $(echo "$description" | sed 's/-/ /; s/-g/-/')
    # Only update if we need to
    if [ "$2 $3" != "$EXIM_RELEASE_VERSION $EXIM_VARIANT_VERSION" ]
    then
            EXIM_RELEASE_VERSION="$2"
            EXIM_VARIANT_VERSION="$3"
            rm -f version.h
    fi
else
    echo "Cannot determine the release number" >&2
    exit
fi

# If you are maintaining a patched version of Exim, you can either
# create your own version.sh as part of your release process, or you
# can modify EXIM_VARIANT_VERSION at this point in this script.

if test -z "$EXIM_RELEASE_VERSION"; then
    echo "$0: Your copy of Exim lacks any version information." >&2
    exit 1
fi

EXIM_COMPILE_NUMBER=$(expr "${EXIM_COMPILE_NUMBER:-0}" + 1)

echo "$EXIM_COMPILE_NUMBER" >cnumber.h

# Reproducible builds, accept a build timestamp override from environ per
# <https://reproducible-builds.org/specs/source-date-epoch/>.
# We require a fairly modern date(1) command here, which is not portable
# to some of the systems Exim is built on.  That's okay, because the scenarios
# are:
#  1) Local postmaster building, not using $SOURCE_DATE_EPOCH, doesn't matter
#  2) Packaging folks who don't care about reproducible builds
#  3) Packaging folks who care but are using systems where date Just Works
#  3) Packaging folks who care and can put a modern date(1) in $PATH
#  4) Packaging folks who care and can supply us with a clean patch to support
#     their requirements
#  5) Packaging folks who care but won't do any work to support their strange
#     old systems and want us to do the work for them.  We don't care either,
#     they're SOL and have to live without reproducible builds.
#
exim_build_date_override=''
if [ ".${SOURCE_DATE_EPOCH:-}" != "." ]; then
  fmt='+%d-%b-%Y %H:%M:%S'
  # Non-reproducible, we use __DATE__ and __TIME__ in C, which respect timezone
  # (think localtime, not gmtime); for reproduction between systems, UTC makes
  # more sense and the examples available use UTC without explicitly mandating
  # it.  I think that we can switch behavior and use UTC for reproducible
  # builds without it causing any problems: nothing really cares about timezone.
  # GNU date: "date -d @TS"
  # BSD date: "date -r TS"
  exim_build_date_override="$(date -u -d "@${SOURCE_DATE_EPOCH}" "$fmt" 2>/dev/null || date -u -r "${SOURCE_DATE_EPOCH}" "$fmt" 2>/dev/null)"
fi

( echo '# automatically generated file - see ../scripts/reversion'
  echo EXIM_RELEASE_VERSION='"'"$EXIM_RELEASE_VERSION"'"'
  test -n "$EXIM_VARIANT_VERSION" && \
  echo EXIM_VARIANT_VERSION='"'"$EXIM_VARIANT_VERSION"'"'
  echo EXIM_COMPILE_NUMBER='"'"$EXIM_COMPILE_NUMBER"'"'
  if [ ".${exim_build_date_override:-}" != "." ]; then
    echo EXIM_BUILD_DATE_OVERRIDE='"'"${exim_build_date_override}"'"'
  fi
) >version.sh

if [ ! -f version.h ]
then
( echo '/* automatically generated file - see ../scripts/reversion */'
  echo '#define EXIM_RELEASE_VERSION "'"$EXIM_RELEASE_VERSION"'"'
  test -n "$EXIM_VARIANT_VERSION" && \
  echo '#define EXIM_VARIANT_VERSION "'"$EXIM_VARIANT_VERSION"'"'
  echo '#ifdef EXIM_VARIANT_VERSION'
  echo '#define EXIM_VERSION_STR EXIM_RELEASE_VERSION "-" EXIM_VARIANT_VERSION'
  echo '#else'
  echo '#define EXIM_VERSION_STR EXIM_RELEASE_VERSION'
  echo '#endif'
  if [ ".${exim_build_date_override:-}" != "." ]; then
    echo '#define EXIM_BUILD_DATE_OVERRIDE "'"${exim_build_date_override}"'"'
  fi
) >version.h
fi

#test -t 1 && echo ">>> version $EXIM_RELEASE_VERSION $EXIM_VARIANT_VERSION #$EXIM_COMPILE_NUMBER"
